package com.es.easystoragesimulatedata.controller;

import com.es.easystoragesimulatedata.model.EnumInfo;
import com.es.easystoragesimulatedata.service.EnumService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.CrossOrigin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 枚举信息控制器
 * 负责处理系统中所有枚举类型的信息查询，包括：
 * - 时间类型枚举
 * - 排名数据类型枚举
 * - 排序类型枚举
 * - 电站状态枚举
 * - 所有枚举信息
 */
@Slf4j
@RestController
@RequestMapping("/api/enums")
@Tag(name = "枚举信息管理", description = "系统枚举类型信息查询相关接口")
@CrossOrigin(origins = "*", allowCredentials = "false", maxAge = 3600)
@RequiredArgsConstructor
public class EnumController {
    
    private final EnumService enumService;
    
    /**
     * 获取时间类型枚举信息
     */
    @GetMapping("/time-types")
    @Operation(
        summary = "获取时间类型枚举", 
        description = "获取系统中所有时间类型的枚举信息，包含枚举键、值和详细描述"
    )
    @ApiOperationSupport(order = 1)
    public ResponseEntity<Map<String, Object>> getTimeTypes() {
        log.info("获取时间类型枚举请求");
        
        try {
            List<EnumInfo> timeTypes = enumService.getTimeTypes();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "获取时间类型枚举成功");
            response.put("data", timeTypes);
            response.put("total", timeTypes.size());
            
            log.info("成功获取时间类型枚举，共{}条记录", timeTypes.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取时间类型枚举失败: {}", e.getMessage(), e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取时间类型枚举失败: " + e.getMessage());
            response.put("data", null);
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取排名数据类型枚举信息
     */
    @GetMapping("/ranking-types")
    @Operation(
        summary = "获取排名数据类型枚举", 
        description = "获取系统中所有排名数据类型的枚举信息，包含枚举键、值和详细描述"
    )
    @ApiOperationSupport(order = 2)
    public ResponseEntity<Map<String, Object>> getRankingDataTypes() {
        log.info("获取排名数据类型枚举请求");
        
        try {
            List<EnumInfo> rankingTypes = enumService.getRankingDataTypes();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "获取排名数据类型枚举成功");
            response.put("data", rankingTypes);
            response.put("total", rankingTypes.size());
            
            log.info("成功获取排名数据类型枚举，共{}条记录", rankingTypes.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取排名数据类型枚举失败: {}", e.getMessage(), e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取排名数据类型枚举失败: " + e.getMessage());
            response.put("data", null);
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取排序类型枚举信息
     */
    @GetMapping("/sort-orders")
    @Operation(
        summary = "获取排序类型枚举", 
        description = "获取系统中所有排序类型的枚举信息，包含枚举键、值和详细描述"
    )
    @ApiOperationSupport(order = 3)
    public ResponseEntity<Map<String, Object>> getSortOrders() {
        log.info("获取排序类型枚举请求");
        
        try {
            List<EnumInfo> sortOrders = enumService.getSortOrders();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "获取排序类型枚举成功");
            response.put("data", sortOrders);
            response.put("total", sortOrders.size());
            
            log.info("成功获取排序类型枚举，共{}条记录", sortOrders.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取排序类型枚举失败: {}", e.getMessage(), e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取排序类型枚举失败: " + e.getMessage());
            response.put("data", null);
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取电站状态枚举信息
     */
    @GetMapping("/station-statuses")
    @Operation(
        summary = "获取电站状态枚举", 
        description = "获取系统中所有电站状态的枚举信息，包含枚举键、值和详细描述"
    )
    @ApiOperationSupport(order = 4)
    public ResponseEntity<Map<String, Object>> getStationStatuses() {
        log.info("获取电站状态枚举请求");
        
        try {
            List<EnumInfo> stationStatuses = enumService.getStationStatuses();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "获取电站状态枚举成功");
            response.put("data", stationStatuses);
            response.put("total", stationStatuses.size());
            
            log.info("成功获取电站状态枚举，共{}条记录", stationStatuses.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取电站状态枚举失败: {}", e.getMessage(), e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取电站状态枚举失败: " + e.getMessage());
            response.put("data", null);
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 获取所有枚举信息
     */
    @GetMapping
    @Operation(
        summary = "获取所有枚举信息", 
        description = "获取系统中所有枚举类型的信息，包括时间类型、排名数据类型、排序类型和电站状态"
    )
    @ApiOperationSupport(order = 5)
    public ResponseEntity<Map<String, Object>> getAllEnums() {
        log.info("获取所有枚举信息请求");
        
        try {
            List<EnumInfo> allEnums = enumService.getAllEnums();
            
            Map<String, Object> response = new HashMap<>();
            response.put("success", true);
            response.put("message", "获取所有枚举信息成功");
            response.put("data", allEnums);
            response.put("total", allEnums.size());
            
            log.info("成功获取所有枚举信息，共{}条记录", allEnums.size());
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            log.error("获取所有枚举信息失败: {}", e.getMessage(), e);
            Map<String, Object> response = new HashMap<>();
            response.put("success", false);
            response.put("message", "获取所有枚举信息失败: " + e.getMessage());
            response.put("data", null);
            return ResponseEntity.internalServerError().body(response);
        }
    }
}
